package com.example.springboot.mapper;

import com.example.springboot.entity.Companies;
import com.example.springboot.entity.students;
import com.example.springboot.entity.user;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select * from users")
    List<user> findAll();

    @Insert("INSERT INTO users (username, password, email, role) " +
            "VALUES (#{username}, #{password}, #{email}, #{role})")
    void addUser(user user);

    @Insert("INSERT INTO students (user_id, name,experience) " +
            "VALUES (#{user_id}, #{name},#{experience})")
    void addStuUser(students student);

    @Insert("INSERT INTO companies (user_id, company_name) " +
            "VALUES (#{user_id}, #{company_name})")
    void addBossUser(Companies companies);


    @Update("UPDATE users SET password=#{password} WHERE user_id=#{user_id}")
    void updateUserByUsername(user user);

    @Update("UPDATE users SET username=#{username}, password=#{password}, email=#{email}, role=#{role} WHERE user_id=#{user_id}")
    void editUserByUsername(user user);

    @Delete("DELETE FROM users WHERE user_id=#{user_id}")
    void deleteUser(int user_id);

    @Delete({
            "<script>",
            "DELETE FROM users WHERE user_id IN ",
            "<foreach item='user_id' collection='list' open='(' separator=',' close=')'>#{user_id}</foreach>",
            "</script>"
    })
    void deleteAccountBatch(List<Long> ids);

    @Delete({
            "<script>",
            "DELETE FROM companies WHERE user_id IN ",
            "<foreach item='user_id' collection='list' open='(' separator=',' close=')'>#{user_id}</foreach>",
            "</script>"
    })
    void deleteAccountBossBatch(List<Long> ids);

    @Delete({
            "<script>",
            "DELETE FROM students WHERE user_id IN ",
            "<foreach item='user_id' collection='list' open='(' separator=',' close=')'>#{user_id}</foreach>",
            "</script>"
    })
    void deleteAccountStuBatch(List<Long> ids);

    @Select("select * from users where username=#{username}")
    List<user> selectByName(String username);

    @Select("SELECT\n" +
            "\tusers.username, \n" +
            "\tusers.email, \n" +
            "\tstudents.*\n" +
            "FROM\n" +
            "\tusers\n" +
            "\tRIGHT JOIN\n" +
            "\tstudents\n" +
            "\tON \n" +
            "\t\tusers.user_id = students.user_id\n" +
            "WHERE\n" +
            "\tusers.user_id = students.user_id")
    List<students> selectStuAll();

    @Select("SELECT\n" +
            "\tusers.username, \n" +
            "\tusers.email, \n" +
            "\tcompanies.*\n" +
            "FROM\n" +
            "\tusers\n" +
            "\tRIGHT JOIN\n" +
            "\tcompanies\n" +
            "\tON \n" +
            "\t\tusers.user_id = companies.user_id\n" +
            "WHERE\n" +
            "\tusers.user_id = companies.user_id")
    List<Companies> selectBossAll();


    @Delete("DELETE FROM companies WHERE user_id=#{user_id}")
    void deleteStuUser(int userId);

    @Delete("DELETE FROM students WHERE user_id=#{user_id}")
    void deleteBossUser(int user_id);
}
